package com.burns.java2d.chapter1;/**
 * Created by burns.
 *
 * @author <a href="http://www.esoon-soft.com/">burns</a>
 * @date 2021/09/17 15:17
 */

import java.awt.*;
import java.awt.event.*;

/**
 * @Description 用户在窗口点击鼠标，圆将移到新的未知，圆心就是用户鼠标的点击位置
 * @ClassName AwtDemo
 * @Author Burns
 * @DAte 2021/9/17 15:17
 */
public class AwtDemo  extends Frame implements ActionListener {
    int x,y = 100;

    public static void main(String[] args){

        Frame frame = new AwtDemo();
        frame.setSize(640,480);
        frame.setVisible(true);
    }

    public AwtDemo() throws HeadlessException {
        setTitle("Awt Demo");
        MenuBar mb = new MenuBar();
        setMenuBar(mb);
        Menu menu = new Menu("File");
        mb.add(menu);
        MenuItem mi = new MenuItem("Exit");
        mi.addActionListener(this);
        menu.add(mi);

        WindowListener windowListener = new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        };
        addWindowListener(windowListener);

        MouseListener mouseListener = new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                x = e.getX();
                y=e.getY();
                repaint();
            }
        };
        addMouseListener(mouseListener);
    }

    @Override
    public void paint(Graphics g) {
        g.drawOval(x-50,y-50,100,100);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String command = e.getActionCommand();
        if("Exit".equals(command)){
            System.exit(0);
        }
    }
}
